//
//  HMMaintabbar.swift
//  swift微博项目
//
//  Created by huodian on 16/9/22.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit

class HMMaintabbar: UITabBar {

    
    var closuer : (()->())?
    
 
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.addSubview(centerBtn)
        
        self.centerBtn.addTarget(self, action: #selector(clickCenterBtn), for: UIControlEvents.touchUpInside)
        
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
  
    func clickCenterBtn() {
        
        closuer?()
        
        
    }
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        var index = 0
        
        let btnW = UIScreen.main.bounds.size.width/5
        
        let btnH = self.bounds.size.height
        
        
        for itemView in self.subviews{
            
            if itemView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                itemView.frame = CGRect(x:CGFloat(index) * btnW,y:0 ,width:btnW,height:btnH)
           
                
                 index += (index == 1) ? 2 : 1
            }

           
            
            
        }
        
        self.centerBtn.bounds.size = CGSize(width:btnW ,height:btnH)
        
        self.centerBtn.center  = CGPoint(x:self.center.x ,y: self.bounds.size.height * 0.5)
        
        
        
    }
    
    lazy var centerBtn : UIButton = {
        
        let btn  = UIButton()
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: UIControlState.normal)
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: UIControlState.normal)
        
        
        
        return btn
        
        
        
        
        
    }()

}
